<?php

namespace app\admin\controller;

use app\admin\model\QuestionBank;
use app\admin\model\Topic;
use think\Controller;
use think\Request;

class TopicController extends Controller
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        //
        $topics = new Topic();
        $topics = $topics->paginate(10);
        return view('index',compact('topics'));
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function add(Request $request)
    {
        $num = ['1'=>'A','2'=>'B','3'=>'C','4'=>'D','5'=>'E','6'=>'F','7'=>'G','8'=>'H','9'=>'I','10'=>'J'];
        $banks = QuestionBank::all();
        if ($request->isPost()) {
            $data = $request->post();
            $validate = $this->validate($data, [
                'title|题目' => 'require',
                'answer|答案' => 'require',
            ]);
            if ($validate !== true) {
                //验证没有通过
                return $this->error($validate);
            }
            //拼接content
            $content = [];
            for ($i = 1 ; $i <= 10 ; $i++){
                $element = 'content'.$i;
                if($data["$element"] != ""){
                    $first = $num[$i];
                    $content["$first"] = "$data[$element]";
                }
            }
            for ($i = 1 ; $i <= 10 ; $i++){
                $element1 = 'content'.$i;
                unset($data["$element1"]);
            }
            $content = json_encode($content,true);
            $data['cotent'] = $content;
            //分割答案
            $data['answer'] = implode(',', str_split($data['answer']));

            $data['addtime'] = time();
            $topic = new Topic();
            $res = $topic->insert($data);
            if ($res) {
                return $this->success('添加成功', 'index');
            } else {
                return $this->error('添加失败', 'add');
            }
        }
        return view('add',compact('banks','num'));
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        //
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        //
        $num = ['1'=>'A','2'=>'B','3'=>'C','4'=>'D','5'=>'E','6'=>'F','7'=>'G','8'=>'H','9'=>'I','10'=>'J'];
        $banks = QuestionBank::all();
        $topic = Topic::get($id);
        //处理题目数组
        $old_content = json_decode($topic->cotent);
        $old_con = [];
        foreach ($num as $k => $v){
            if (isset($old_content->$v)){
                $old_con["$k"] = $old_content->$v;
            }else{
                $old_con["$k"] = "";
            }
        }
        $old_answer = explode(',',$topic->answer);
        $str = '';
        foreach ($old_answer as $old_an){
            $str = $str.$old_an;
        }
        $topic->answer = $str;
        return view('read',compact('banks','num','topic'));
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit(Request $request,$id)
    {
        //
        $num = ['1'=>'A','2'=>'B','3'=>'C','4'=>'D','5'=>'E','6'=>'F','7'=>'G','8'=>'H','9'=>'I','10'=>'J'];
        $banks = QuestionBank::all();
        $topic = Topic::get($id);
        //处理题目数组
        $old_content = json_decode($topic->cotent);
        $old_con = [];
        foreach ($num as $k => $v){
            if (isset($old_content->$v)){
                $old_con["$k"] = $old_content->$v;
            }else{
                $old_con["$k"] = "";
            }
        }
        $old_answer = explode(',',$topic->answer);
        $str = '';
        foreach ($old_answer as $old_an){
            $str = $str.$old_an;
        }
        $topic->answer = $str;
        if ($request->isPost()) {
            $data = $request->post();
            $validate = $this->validate($data, [
                'title|题目' => 'require',
                'answer|答案' => 'require',
            ]);
            if ($validate !== true) {
                //验证没有通过
                return $this->error($validate);
            }
            //拼接content
            $content = [];
            for ($i = 1 ; $i <= 10 ; $i++){
                $element = 'content'.$i;
                if($data["$element"] != ""){
                    $first = $num[$i];
                    $content["$first"] = "$data[$element]";
                }
            }
            for ($i = 1 ; $i <= 10 ; $i++){
                $element1 = 'content'.$i;
                unset($data["$element1"]);
            }
            $content = json_encode($content,true);
            $data['cotent'] = $content;
            //分割答案
            $data['answer'] = implode(',', str_split($data['answer']));

            $data['addtime'] = time();
            $res = $topic->allowField(true)->save($data);
            if ($res) {
                return $this->success('修改成功', 'index');
            } else {
                return $this->error('修改失败', 'edit');
            }
        }
        return view('edit',compact('banks','num','topic','old_con'));
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function del($id)
    {
        //
        $topic = Topic::get($id);
        $res = $topic->delete();
        if ($res) {
            return $this->success('删除成功', 'index');
        } else {
            return $this->error('删除失败', 'index');
        }
    }
}
